package com.doudizu.shoesonline.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doudizu.shoesonline.common.BaseResponse;
import com.doudizu.shoesonline.common.ResultUtils;
import com.doudizu.shoesonline.model.entity.Address;
import com.doudizu.shoesonline.service.AddressService;
import com.doudizu.shoesonline.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ln
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地址簿模块")
public class AddressController {

    @Resource
    private AddressService addressService;

    @Resource
    private UserService userService;

    @GetMapping("/list")
    @ApiOperation("查询所有地址")
    public BaseResponse list() {
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("user_id",userService.getLoginUser().getId());
        List<Address> list = addressService.list(addressQueryWrapper);
        return ResultUtils.success(list);
    }

    @GetMapping
    @ApiOperation("查询单个地址")
    public BaseResponse get(Integer id) {
        Address address = addressService.getById(id);
        return ResultUtils.success(address);
    }

    @PostMapping("/add")
    @ApiOperation("添加地址")
    public BaseResponse add(@RequestBody Address address) {
        address.setUserId(userService.getLoginUser().getId());
        addressService.save(address);
        return ResultUtils.success(null);
    }

    @PostMapping("/update")
    @ApiOperation("更新地址")
    public BaseResponse update(@RequestBody Address address) {
        addressService.updateById(address);
        return ResultUtils.success(null);
    }

    @PostMapping("/delete")
    @ApiOperation("删除地址")
    public BaseResponse delete(Integer id) {
        addressService.removeById(id);
        return ResultUtils.success(null);
    }
}

